package com.eccg.shopping.activity;

import java.io.File;

import com.eccg.shopping.R;
import com.eccg.shopping.utils.Store;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class TingcheActivity extends BaseActivity {

	private ImageView tingche_qr_iv;
	private ImageView tingche_photo_iv;

	private final int QRCODE = 0;
	private final int CAMERA = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tingche);

		tingche_qr_iv = (ImageView) findViewById(R.tingche.tingche_qr_iv);
		tingche_photo_iv = (ImageView) findViewById(R.tingche.tingche_photo_iv);

		tingche_qr_iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent("com.eccg.shopping.SCAN");
				startActivityForResult(intent, QRCODE);
			}
		});

		tingche_photo_iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				File path = new File(Environment.getExternalStorageDirectory() + "/xuepin/imageCache/");
				if (!path.exists())
					path.mkdirs();
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path, "temp.jpg")));
				startActivityForResult(intent, CAMERA);
			}
		});
	}

	/**
	 * qr码返回
	 */
	public static String $contents;

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == QRCODE) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				$contents = contents;
				if (contents != null && !"".equals(contents)) {
					Toast.makeText(TingcheActivity.this, contents, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(TingcheActivity.this, "该二维码信息不能识别", Toast.LENGTH_SHORT).show();
				}
			} else if (resultCode == RESULT_CANCELED) {
			}
		} else if (requestCode == CAMERA) {
			if (resultCode == RESULT_OK) {
				File path = new File(Environment.getExternalStorageDirectory() + "/xuepin/imageCache/");
				File picture = new File(path, "temp.jpg");
			}
		}
	}

	@Override
	protected int getMenuIndex() {
		return 2;
	}

	@Override
	protected void initHeader() {
		super.initHeader();
		titleTx.setText("停车");
		Button backBtn = new Button(this);
		backBtn.setText("返回");
		backBtn.setTextSize(16);
		backBtn.setTextColor(getResources().getColor(R.color.white));
		backBtn.setGravity(Gravity.CENTER);
		backBtn.setBackgroundResource(R.drawable.left_btn_bg);
		backBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		leftTitle.removeAllViews();
		leftTitle.addView(backBtn);
	}

}
